package com.eagledata.project.business.image.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.eagledata.project.business.image.domain.Image;
import com.eagledata.project.business.image.service.IImageService;
import com.eagledata.common.utils.FileUploadUtils;
import com.eagledata.common.utils.security.ShiroUtils;
import com.eagledata.framework.config.EagleDataConfig;
import com.eagledata.framework.web.controller.BaseController;
import com.eagledata.framework.web.page.TableDataInfo;
import com.eagledata.framework.web.domain.Message;

/**
 * 图片 信息操作处理
 * 
 * @author eagledata
 * @date 2018-07-02
 */
@Controller
@RequestMapping("/business/image")
public class ImageController extends BaseController {
    private String prefix = "business/image";
	
	@Autowired
	private IImageService imageService;
	
	@RequestMapping()
	@RequiresPermissions("business:image:view")
	public String image() {
	    return prefix + "/image";
	}
	
	/**
	 * 查询图片列表
	 */
	@RequiresPermissions("business:image:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(Image image) {
		startPage();
        List<Image> list = imageService.selectImageList(image);
		return getDataTable(list);
	}
	
	/**
	 * 新增图片
	 */
	@RequiresPermissions("business:image:add")
	@RequestMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 修改图片
	 */
	@RequiresPermissions("business:image:edit")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		Image image = imageService.selectImageById(id);
		model.addAttribute("image", image);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存图片
	 */
	@RequiresPermissions("business:image:save")
	@RequestMapping("/save")
	@ResponseBody
	public Message save(Image image) {
		if (imageService.saveImage(image) > 0)
		{
			return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 删除图片
	 */
	@RequiresPermissions("business:image:remove")
	@RequestMapping( "/remove/{id}")
	@ResponseBody
	public Message remove(@PathVariable("id") Integer id) {
		if (imageService.deleteImageById(id) > 0)
		{
		    return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除图片
	 */
	@RequiresPermissions("business:image:batchRemove")
	@RequestMapping( "/batchRemove")
	@ResponseBody
	public Message remove(@RequestParam("ids[]") Integer[] ids) {
		int rows = imageService.batchDeleteImage(ids);
		if (rows > 0)
        {
            return Message.success();
        }
        return Message.error();
	}
	
	
	/**
	 * 图片上传
	 * @return
	 */
	@RequestMapping("uploadImage")
	@ResponseBody
	public Image uploadImage(MultipartFile file, Image image) {
		if(null==file || null == image) {
			return null;
		}
		try {
			String baseDir = EagleDataConfig.getProfile();
			String extension = FileUploadUtils.getFileSuffix(file.getOriginalFilename());
			String fileName = FileUploadUtils.upload(baseDir, file, extension);
			image.setCreateBy(ShiroUtils.getLoginName());
			image.setCreateTime(new Date());
			image.setUrl(fileName);
			imageService.saveImage(image);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return image ;
	}
	
}
